//
//  FirstViewController.m
//  EducationLive
//
//  Created by Apple on 2017/9/26.
//  Copyright © 2017年 ZhouHao. All rights reserved.
//
/**
 *  成功返回值
 */
#define SucessBack 0
#define FailError  @"网络或者服务器错误"

#import "MAAFRequest.h"

@implementation MAAFRequest
// 头文件配置
+ (AFHTTPSessionManager *)baseHtppRequest:(BOOL)networkState {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    // https
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy defaultPolicy];
    securityPolicy.allowInvalidCertificates = YES;
    manager.securityPolicy = securityPolicy;
    
    // 添加AFN没有的属性@"text/html"
    // 以下为修改属性,修改设置前最好先new 一下,防止修改无效
    manager.responseSerializer = [AFJSONResponseSerializer new];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
    // timeout设置
    [manager.requestSerializer setTimeoutInterval:TIMEOUT];
    // header 设置
    //[manager.requestSerializer setValue:@"iphone" forHTTPHeaderField:@"client_type"];
    // 判断网络情况
    if (networkState) {
        manager.requestSerializer.cachePolicy =  NSURLRequestReloadIgnoringLocalCacheData;
    }else{
        manager.requestSerializer.cachePolicy =  NSURLRequestReturnCacheDataElseLoad;
    }
    
    return manager;
}
// 缓存数据
+ (void)dataCache:(id)responseObject task:(NSURLSessionDataTask *_Nonnull) task
{
    NSData *data = [NSJSONSerialization dataWithJSONObject:responseObject options:0 error:nil];
    NSURLCache *URLCache = [NSURLCache sharedURLCache];
    NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:task.response data:data];
    [URLCache storeCachedResponse:cachedResponse forRequest:task.originalRequest];
}

// 首页栏目
+ (void)getColumn:(NSDictionary *)userInfo networkState:(BOOL) networkState successBlock:(SuccessBlock )successBlock failureBlock:(FailureBlock)failureBlock {
    AFHTTPSessionManager *manager = [self baseHtppRequest:networkState];
    NSString *url = [NSString stringWithFormat:@"%@%@",HostURLString,@"Index/column"];
    [manager GET:url parameters:userInfo progress:^(NSProgress * _Nonnull downloadProgress) {
        // 接口访问进度
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 缓存数据
        [self dataCache:responseObject task:task];
        // 接口访问成功
        successBlock(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        // 接口访问失败
        failureBlock(FailError);
    }];
}

// 首页数据
+ (void)getIndex:(NSDictionary *)userInfo networkState:(BOOL) networkState successBlock:(SuccessBlock )successBlock failureBlock:(FailureBlock)failureBlock {
    AFHTTPSessionManager *manager = [self baseHtppRequest:networkState];
    NSString *url = [NSString stringWithFormat:@"%@%@",HostURLString,@"Index/index"];
    [manager GET:url parameters:userInfo progress:^(NSProgress * _Nonnull downloadProgress) {
        // 接口访问进度
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 缓存数据
        [self dataCache:responseObject task:task];
        // 接口访问成功
        successBlock(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        // 接口访问失败
        failureBlock(FailError);
    }];
}

@end
